<?php

//运单控制器
class WaybillApp extends BackendApp
{
	var $_waybill_mod;
	var $_dictionary_mod;
	var $_user_mod;
	var $canedit;
	var $_haul_mod;
	var $_exception_mod;
	var $_exattr_mod;

	function __construct()
	{
		$this->WaybillApp();
	}

	function WaybillApp()
	{
		parent::BackendApp();

		$this->_waybill_mod =& m('waybill');
		$this->_dictionary_mod =& m('dictionary');
		$this->_user_mod =& m('member');
		$this->_haul_mod =& m('haul');
		$this->_exception_mod =& m('exception');
		$this->_exattr_mod =& m('exattr');
		$this->canedit=false;
	}
	
	
function view(){
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!IS_POST){
			$waybill=$this->_waybill_mod->get(array(
	        'fields'   => 'this.*,member.user_name,member.real_name',
	        'conditions'  => 'wm_tid='.$id,
			'join'=>'belongs_to_user',
			));
			$db=&db();
			$normal=true;
			if($waybill['wm_type']=='集卡进口'||$waybill['wm_type']=='集卡出口'){
				$normal=false;
				$sql="SELECT * from zys_waybill_sub_trunk w LEFT JOIN zys_goods_trunk_sub g on w.ws_gstid=g.gs_tid LEFT JOIN zys_goods_trunk t on t.g_tid=g.gs_gtid where ws_wmtid=".$id;
				$sub=$db->getAll($sql);
				$sublist=array();
				if($sub){
					foreach ($sub as $v){
						$sublist[$v['gs_gtid']]['g_tid']=$v['g_tid'];
						$sublist[$v['gs_gtid']]['g_companytid']=$v['g_companytid'];
						$sublist[$v['gs_gtid']]['g_ship']=$v['g_ship'];
						$sublist[$v['gs_gtid']]['g_voyage']=$v['g_voyage'];
						$sublist[$v['gs_gtid']]['g_awb_code']=$v['g_awb_code'];
						$sublist[$v['gs_gtid']]['g_packingtime']=$v['g_packingtime'];
						$sublist[$v['gs_gtid']]['g_collectiontime']=$v['g_collectiontime'];
						$sublist[$v['gs_gtid']]['g_container_depot']=$v['g_container_depot'];
						$sublist[$v['gs_gtid']]['g_container_lng']=$v['g_container_lng'];
						$sublist[$v['gs_gtid']]['g_container_lat']=$v['g_container_lat'];
						$sublist[$v['gs_gtid']]['g_container_man']=$v['g_container_man'];
						$sublist[$v['gs_gtid']]['g_container_tel']=$v['g_container_tel'];
						$sublist[$v['gs_gtid']]['g_full_container_depot']=$v['g_full_container_depot'];
						$sublist[$v['gs_gtid']]['g_full_container_lng']=$v['g_full_container_lng'];
						$sublist[$v['gs_gtid']]['g_full_container_lat']=$v['g_full_container_lat'];
						$sublist[$v['gs_gtid']]['g_full_container_man']=$v['g_full_container_man'];
						$sublist[$v['gs_gtid']]['g_full_container_tel']=$v['g_full_container_tel'];
						$sublist[$v['gs_gtid']]['g_loading_place']=$v['g_loading_place'];
						$sublist[$v['gs_gtid']]['g_loading_lng']=$v['g_loading_lng'];
						$sublist[$v['gs_gtid']]['g_loading_lat']=$v['g_loading_lat'];
						$sublist[$v['gs_gtid']]['g_loading_man']=$v['g_loading_man'];
						$sublist[$v['gs_gtid']]['g_loading_tel']=$v['g_loading_tel'];
						$sublist[$v['gs_gtid']]['g_note']=$v['g_note'];
						$sublist[$v['gs_gtid']]['g_tgdate']=$v['g_tgdate'];
						$sublist[$v['gs_gtid']]['box'][]=$v;
					}
				}
			}else{
				$sql="SELECT * from zys_waybill_sub_normal w LEFT JOIN zys_goods_normal_sub g on w.ws_gstid=g.gs_tid LEFT JOIN zys_goods_normal t on t.g_tid=g.gs_gtid where ws_wmtid=".$id;
				$sub=$db->getAll($sql);
				$sublist=array();
				if($sub){
					foreach ($sub as $v){
						$sublist[$v['gs_gtid']]['g_tid']=$v['g_tid'];
						$sublist[$v['gs_gtid']]['g_ownertid']=$v['g_ownertid'];
						$sublist[$v['gs_gtid']]['g_owner']=$v['g_owner'];
						$sublist[$v['gs_gtid']]['g_owner_tel']=$v['g_owner_tel'];
						$sublist[$v['gs_gtid']]['g_loading_place']=$v['g_loading_place'];
						$sublist[$v['gs_gtid']]['g_loading_lng']=$v['g_loading_lng'];
						$sublist[$v['gs_gtid']]['g_loading_lat']=$v['g_loading_lat'];
						$sublist[$v['gs_gtid']]['g_unloading_place']=$v['g_unloading_place'];
						$sublist[$v['gs_gtid']]['g_loading_date']=$v['g_loading_date'];
						$sublist[$v['gs_gtid']]['g_unloading_date']=$v['g_unloading_date'];
						$sublist[$v['gs_gtid']]['g_unloading_lng']=$v['g_unloading_lng'];
						$sublist[$v['gs_gtid']]['g_unloading_lat']=$v['g_unloading_lat'];
						$sublist[$v['gs_gtid']]['g_consignee']=$v['g_consignee'];
						$sublist[$v['gs_gtid']]['g_consignee_tel']=$v['g_consignee_tel'];
						$sublist[$v['gs_gtid']]['g_gtype']=$v['g_gtype'];
						$sublist[$v['gs_gtid']]['g_consignor']=$v['g_consignor'];
						$sublist[$v['gs_gtid']]['g_consignor_tel']=$v['g_consignor_tel'];
						$sublist[$v['gs_gtid']]['g_note']=$v['g_note'];
						$sublist[$v['gs_gtid']]['box'][]=$v;
					}
				}
			}

			$sql="SELECT d_value,d_key FROM zys_dictionary WHERE d_code='haultype'";
			$typelist=array();
			$temp=$db->getAll($sql);

			foreach ($temp as $v){
				$typelist[$v['d_key']]=$v['d_value'];
			}
			$this->assign('typelist', $typelist);

			$sql="SELECT d_value,d_key FROM zys_dictionary WHERE d_code='h_collect_type'";
			$typelist=array();
			$temp=$db->getAll($sql);

			foreach ($temp as $v){
				$typelist[$v['d_key']]=$v['d_value'];
			}
			$this->assign('ctype', $typelist);
			$sql="SELECT * from zys_haul where h_wmtid=".$id." ORDER BY h_order";
			$haul=$db->getAll($sql);

			$haullist=array();
			if($haul){
				foreach ($haul as $v){
					$sql="SELECT * from zys_haul_collect WHERE hc_h_tid=".$v['h_tid'];
					$collect=$db->getAll($sql);
					if($collect){
						foreach ($collect as $vv){
							$arrs=array();
							if($vv['h_collect_val']){
								$vv['h_collect_val']=trim($vv['h_collect_val'],',');
								$arrs=explode(',', $vv['h_collect_val']);
							}
							$vv['ar']=$arrs;
							$v['item'][]=$vv;
						}
					}

					$haullist[]=$v;
				}
			}
			//异常信息
			$exception=array();
			$sql="SELECT * FROM zys_exception WHERE e_w_tid=$id";
			$exception=$db->getAll($sql);
			if($exception){
				foreach ($exception as $k=>$v){
					$sql="SELECT * FROM zys_exception_attr WHERE ea_type=0 and ea_e_tid=".$v['e_tid'];
					$exception[$k]['e_pic']=$db->getAll($sql);

					$sql="SELECT * FROM zys_exception_attr WHERE ea_type!=0 and ea_e_tid=".$v['e_tid'];
					$exception[$k]['e_voice']=$db->getAll($sql);
				}
			}
			$this->assign('exception', $exception);
			$this->assign('haullist', $haullist);
			$this->assign('normal', $normal);
			$this->assign('waybill', $waybill);
			$this->assign('sublist', $sublist);
			/* 导入jQuery的表单验证插件 */
			$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js,jquery.plugins/jquery.validate.js',
				'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
			$this->display('waybill.view.html');
		}
	}
	
	function edit()
	{
		$db=&db();
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		$waybill=$this->_waybill_mod->get(array(
	        'fields'   => 'this.*',
	        'conditions'  => 'wm_tid='.$id,
		));
		if($waybill['wm_stauts']=='新建'||$waybill['wm_stauts']=='已接单'||$waybill['wm_stauts']=='已派'){
			$this->canedit=true;
		}
		if (!IS_POST)
		{
			//异常信息
			$exception=array();
			$sql="SELECT * FROM zys_exception WHERE e_w_tid=$id";
			$exception=$db->getAll($sql);
			if($exception){
				foreach ($exception as $k=>$v){
					$sql="SELECT * FROM zys_exception_attr WHERE ea_type=0 and ea_e_tid=".$v['e_tid'];
					$exception[$k]['e_pic']=$db->getAll($sql);

					/*$sql="SELECT * FROM zys_exception_attr WHERE ea_type!=0 and ea_e_tid=".$v['e_tid'];
					 $exception[$k]['e_voice']=$db->getAll($sql);*/
				}
			}
			$this->assign('exception', $exception);

			//------------------编辑状态下运营商列表start--------------------
			$sql="SELECT * FROM zys_operator WHERE o_carrier=1";
			$oper=$db->getAll($sql);
			$oarray=array();
			foreach ($oper as $v){
				$oarray[$v['o_tid']]=$v['o_name'];
			}
			$this->assign('oper', $oarray);
			$unit = $this->visitor->get('unit');
			$this->assign('unit', $unit);
			//------------------编辑状态下运营商列表end----------------------

			$waybill=$this->_waybill_mod->get(array(
	        'fields'   => 'this.*,member.user_name',
	        'conditions'  => 'wm_tid='.$id,
			'join'=>'belongs_to_user',
			));

			$types=array();
			$types['集卡进口'] = '集卡进口';
			$types['集卡出口'] = '集卡出口';
			$types['整车'] = '整车';
			$types['零担'] = '零担';
			$types['城配'] = '城配';
			$this->assign('search_options', $types);
			$this->assign('id', $id);
			$this->assign('waybill', $waybill);
			$this->assign('wm_id', $waybill['wm_tid']);
			$dictionary_mod=&m('dictionary');
			$boxtype=array();
			$boxtypeinfo=$dictionary_mod->find("d_code='boxtype'");
			foreach($boxtypeinfo as $key=>$val){
				$boxtype[$val['d_key']]=$val['d_value'];
			}
			$sql="select * from zys_operator where o_consignor=1";
			$huo=$db->getAll($sql);
			$h=array();
			foreach ($huo as $v){
				$h[$v['o_tid']]=$v['o_name'];
			}
			$this->assign('hhh', $h);
			$this->assign('box_options', $boxtype);
			/* 导入jQuery的表单验证插件 */
			$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js,jquery.plugins/jquery.validate.js',
        	'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));
			$normal=true;
			if($waybill['wm_type']=='集卡进口'||$waybill['wm_type']=='集卡出口'){
				$normal=false;
			}
			$this->assign('normal', $normal);
			$sql="select * from zys_waybill_sub_normal where ws_wmtid=".$id;
			$sub_normal =$db->getAll($sql);
			$this->assign('sub_normal', $sub_normal);
			$sql="select * from zys_waybill_sub_trunk where ws_wmtid=".$id;
			$sub_trunk =$db->getAll($sql);
			$this->assign('sub_trunk', $sub_trunk);
			$this->assign('tabself', json_encode(array('text'=>'编辑运单')));
			$this->assign('tabback', json_encode(array('text'=>'运单管理','url'=>'')));
			$cannew=true;
			$canyy=true;

			$sql="SELECT * from zys_waybill_sub_trunk w
				  LEFT JOIN zys_goods_trunk_sub g on w.ws_gstid=g.gs_tid
				  LEFT JOIN zys_goods_trunk t on t.g_tid=g.gs_gtid where
				  ws_wmtid=".$id;

			$sub=$db->getAll($sql);
			$sublist=array();
			$consignor =array();
			if($sub){
				$consignor = array('0') ;
				foreach ($sub as $v){
					if($v['ws_new']){
						$cannew=false;
					}
					if(!$v['ws_new']){
						$canyy=false;
					}
					$consignor[] =intval($v['g_companytid']) ;
					$sublist[$v['gs_gtid']]['g_tid']=$v['g_tid'];
					$sublist[$v['gs_gtid']]['g_companytid']=$v['g_companytid'];
					$sublist[$v['gs_gtid']]['g_ship']=$v['g_ship'];
					$sublist[$v['gs_gtid']]['g_voyage']=$v['g_voyage'];
					$sublist[$v['gs_gtid']]['g_awb_code']=$v['g_awb_code'];
					$sublist[$v['gs_gtid']]['g_packingtime']=$v['g_packingtime'];
					$sublist[$v['gs_gtid']]['g_collectiontime']=$v['g_collectiontime'];
					$sublist[$v['gs_gtid']]['g_container_depot']=$v['g_container_depot'];
					$sublist[$v['gs_gtid']]['g_container_lng']=$v['g_container_lng'];
					$sublist[$v['gs_gtid']]['g_container_lat']=$v['g_container_lat'];
					$sublist[$v['gs_gtid']]['g_container_man']=$v['g_container_man'];
					$sublist[$v['gs_gtid']]['g_container_tel']=$v['g_container_tel'];
					$sublist[$v['gs_gtid']]['g_full_container_depot']=$v['g_full_container_depot'];
					$sublist[$v['gs_gtid']]['g_full_container_lng']=$v['g_full_container_lng'];
					$sublist[$v['gs_gtid']]['g_full_container_lat']=$v['g_full_container_lat'];
					$sublist[$v['gs_gtid']]['g_full_container_man']=$v['g_full_container_man'];
					$sublist[$v['gs_gtid']]['g_full_container_tel']=$v['g_full_container_tel'];
					$sublist[$v['gs_gtid']]['g_loading_place']=$v['g_loading_place'];
					$sublist[$v['gs_gtid']]['g_loading_lng']=$v['g_loading_lng'];
					$sublist[$v['gs_gtid']]['g_loading_lat']=$v['g_loading_lat'];
					$sublist[$v['gs_gtid']]['g_loading_man']=$v['g_loading_man'];
					$sublist[$v['gs_gtid']]['g_loading_tel']=$v['g_loading_tel'];
					$sublist[$v['gs_gtid']]['g_note']=$v['g_note'];
						
					$sublist[$v['gs_gtid']]['g_tgdate']=$v['g_tgdate'];
						
					$sublist[$v['gs_gtid']]['box'][]=$v;
				}
			}
			$this->assign('cannew', $cannew);
			$this->assign('canyy', $canyy);
			if(empty($sublist)){
				$sublist=array('g_tid'=>0);
			}
			$this->assign('sub', $sublist);

			//当前单位下的，并且包含货单中的货主的
			$unit = $this->visitor->get('unit');
			$consignors = implode(',',$consignor);
			$mwhere = "1=1";
			if(!empty($unit)){
				$mwhere .=" and zmm_otid=".$unit;
			}
			if(!empty($consignors)){
				$mwhere .=" and (zmm_consignor in (".$consignors."))";
			}
			$monitor=array();
			$sql="select * from zys_monitor_main where ".$mwhere;
			$m=$db->getAll($sql);
			if($m){
				foreach ($m as $v){
					$monitor[$v['zmm_tid']]=$v['zmm_name'];
				}
			}
			$this->assign('monitor', $monitor);
			$hwhere =' and zhdm_userid='.$this->visitor->get('user_id');
			if(!empty($unit)){
				$hwhere .=" and zhdm_otid=".$unit;
			}
			if(!empty($consignors)){
				$hwhere .=" and (zhdm_consignor in (".$consignors."))";
			}
			$hauldefine=array();
			$sql="select * from zys_haul_define_main where zhdm_type='".$waybill['wm_type']."'".$hwhere;
			$h=$db->getAll($sql);
			if($h){
				foreach ($h as $v){
					$hauldefine[$v['zhdm_tid']]=$v['zhdm_name'];
				}
			}
			$this->assign('hauldefine', $hauldefine);
			$sql="SELECT d_value,d_key FROM zys_dictionary WHERE d_code='haultype'";
			$typelist=array();
			$temp=$db->getAll($sql);

			foreach ($temp as $v){
				$typelist[$v['d_key']]=$v['d_value'];
			}
			$this->assign('typelist', $typelist);
			$sql="SELECT d_value,d_key FROM zys_dictionary WHERE d_code='h_collect_type'";
			$typelist=array();
			$temp=$db->getAll($sql);

			foreach ($temp as $v){
				$typelist[$v['d_key']]=$v['d_value'];
			}
			$this->assign('ctype', $typelist);
			$haullist=array();
			$sql="SELECT * from zys_haul where h_wmtid=".$id." ORDER BY h_order";
			$haul=$db->getAll($sql);

			$haullist=array();
			if($haul){
				foreach ($haul as $v){
					$sql="SELECT * from zys_haul_collect WHERE hc_h_tid=".$v['h_tid'];
					$collect=$db->getAll($sql);
					if($collect){
						foreach ($collect as $vv){
							$arrs=array();
							if($vv['h_collect_val']){
								$vv['h_collect_val']=trim($vv['h_collect_val'],',');
								$arrs=explode(',', $vv['h_collect_val']);
							}
							$vv['ar']=$arrs;
							$v['item'][]=$vv;
						}
					}

					$haullist[]=$v;
				}
			}


			$this->assign('canedit', $this->canedit);
			$this->assign('haullist', $haullist);

			$this->display('waybill.formcopy.html');
		}else
		{
			$sql='select wm_version from zys_waybill_main where wm_tid='.$id;
			$wm_version = $this->_waybill_mod->getOne($sql);

			if(intval($wm_version)<=0){
				$wm_version=0;
			}
			$wm_version=$wm_version+1;


			if($this->canedit){

				$gs_tid=empty($_POST['gs_tid'])?array():$_POST['gs_tid'];
				$g_box=empty($_POST['g_box'])?array():$_POST['g_box'];
				$g_volume=empty($_POST['g_volume'])?array():$_POST['g_volume'];
				$g_ctn_no=empty($_POST['g_ctn_no'])?array():$_POST['g_ctn_no'];

				$g_tid=empty($_POST['g_tid'])?0:$_POST['g_tid'];

				if(empty($g_box)||empty($g_volume)||empty($g_ctn_no)){
					$this->show_warning('请填写集装箱','',   'index.php?app=waybill&act=edit&id='.$id);
					return;
				}

				$g_companytid=empty($_POST['g_companytid'])?0: $_POST['g_companytid'];
				$sql="select * from zys_operator where o_tid=".$g_companytid;
				$operator=$db->getRow($sql);

				$g_ship=empty($_POST['g_ship'])?'':$_POST['g_ship'];
				$g_voyage=empty($_POST['g_voyage'])?'':$_POST['g_voyage'];
				$g_awb_code=empty($_POST['g_awb_code'])?'':$_POST['g_awb_code'];
				$g_packingtime=empty($_POST['g_packingtime'])?'':$_POST['g_packingtime'];
				if(!empty($g_packingtime)){
					$g_packingtime=gmstr2time($g_packingtime);
				}
					
				$g_tgdate=empty($_POST['g_tgdate'])?'':$_POST['g_tgdate'];
				if(!empty($g_tgdate)){
					$g_tgdate=gmstr2time($g_tgdate);
				}



				$g_collectiontime=empty($_POST['g_collectiontime'])?'':$_POST['g_collectiontime'];
				if(!empty($g_collectiontime)){
					$g_collectiontime=gmstr2time($g_collectiontime);
				}

				$g_container_depot=empty($_POST['g_container_depot'])?'':$_POST['g_container_depot'];
				$g_container_lng=empty($_POST['g_container_lng'])?'':$_POST['g_container_lng'];
				$g_container_lat=empty($_POST['g_container_lat'])?'':$_POST['g_container_lat'];
				if(empty($g_container_depot)){
					$this->show_warning('请填写空箱场站', '',  'index.php?app=waybill&act=add');
					return;
				}
				$g_container_man=empty($_POST['g_container_man'])?'':$_POST['g_container_man'];
				if(empty($g_container_man)){
					$this->show_warning('请填写空箱场站联系人', '',  'index.php?app=waybill&act=add');
					return;
				}
				$g_container_tel=empty($_POST['g_container_tel'])?'':$_POST['g_container_tel'];
				if(empty($g_container_tel)){
					$this->show_warning('请填写空箱场站电话', '',  'index.php?app=waybill&act=add');
					return;
				}

				$g_full_container_depot=empty($_POST['g_full_container_depot'])?'':$_POST['g_full_container_depot'];
				$g_full_container_lng=empty($_POST['g_full_container_lng'])?'':$_POST['g_full_container_lng'];
				$g_full_container_lat=empty($_POST['g_full_container_lat'])?'':$_POST['g_full_container_lat'];
				if(empty($g_full_container_depot)){
					$this->show_warning('请填写重箱场站', '',  'index.php?app=waybill&act=add');
					return;
				}
				$g_full_container_man=empty($_POST['g_full_container_man'])?'':$_POST['g_full_container_man'];
				if(empty($g_full_container_man)){
					$this->show_warning('请填写重箱场站联系人', '',  'index.php?app=waybill&act=add');
					return;
				}
				$g_full_container_tel=empty($_POST['g_full_container_tel'])?'':$_POST['g_full_container_tel'];
				if(empty($g_full_container_tel)){
					$this->show_warning('请填写重箱场站电话', '',  'index.php?app=waybill&act=add');
					return;
				}
				$g_loading_place=empty($_POST['g_loading_place'])?'':$_POST['g_loading_place'];
				$g_loading_lng=empty($_POST['g_loading_lng'])?'':$_POST['g_loading_lng'];
				$g_loading_lat=empty($_POST['g_loading_lat'])?'':$_POST['g_loading_lat'];
				if(empty($g_loading_place)){
					$this->show_warning('请填写装货地','',   'index.php?app=waybill&act=edit&id='.$id);
					return;
				}

				$g_loading_man=empty($_POST['g_loading_man'])?'':$_POST['g_loading_man'];
				if(empty($g_loading_man)){
					$this->show_warning('请填写装货联系人','',   'index.php?app=waybill&act=edit&id='.$id);
					return;
				}

				$g_loading_tel=empty($_POST['g_loading_tel'])?'':$_POST['g_loading_tel'];
				if(empty($g_loading_tel)){
					$this->show_warning('请填写装货联系电话','',   'index.php?app=waybill&act=edit&id='.$id);
					return;
				}
				$g_note=empty($_POST['g_note'])?'':$_POST['g_note'];

				$wm_type=empty($_POST['wm_type'])?'':$_POST['wm_type'];
				if(empty($wm_type)){
					$this->show_warning('请选择运单类型','',   'index.php?app=waybill&act=edit&id='.$id);
					return;
				}
				$userid = $this->visitor->get('user_id');
				$unit = $this->visitor->get('unit');
				if($_POST['wm_type']=='集卡进口'){
					$num = $this->checkaddr($_POST['g_container_depot'],$userid,$unit);
					if($num==0){
						$depot = array(
							'user_id'    	=>	$userid,
							'consignee' 	=>	$_POST['g_container_man'],
							'address' 		=>	$_POST['g_container_depot'],
							'phone_tel'	=>	$_POST['g_container_tel'],
							'lng'			=>	$_POST['g_container_lng'],
							'lat'			=>	$_POST['g_container_lat'],
							'addrtype'		=>	'集进空站',
							'unit'			=>	$unit,
						);
						$this->addaddr($depot);
					}
					$num1 = $this->checkaddr($_POST['g_full_container_depot'],$userid,$unit);
					if($num1==0){
						$full_depot = array(
							'user_id'    	=>	$userid,
							'consignee' 	=>	$_POST['g_full_container_man'],
							'address' 		=>	$_POST['g_full_container_depot'],
							'phone_tel'	=>	$_POST['g_full_container_tel'],
							'lng'			=>	$_POST['g_full_container_lng'],
							'lat'			=>	$_POST['g_full_container_lat'],
							'addrtype'		=>	'集进重站',
							'unit'			=>	$unit,
						);
						$this->addaddr($full_depot);
					}
					$num2 = $this->checkaddr($_POST['g_loading_place'],$userid,$unit);
					if($num2==0){
						$place = array(
							'user_id'    	=>	$userid,
							'consignee' 	=>	$_POST['g_loading_man'],
							'address' 		=>	$_POST['g_loading_place'],
							'phone_tel'	=>	$_POST['g_loading_tel'],
							'lng'			=>	$_POST['g_loading_lng'],
							'lat'			=>	$_POST['g_loading_lat'],
							'addrtype'		=>	'集进卸货',
							'unit'			=>	$unit,
						);
						$this->addaddr($place);
					}
				}else{
					$num = $this->checkaddr($_POST['g_container_depot'],$userid,$unit);
					if($num==0){
						$depot = array(
							'user_id'    	=>	$userid,
							'consignee' 	=>	$_POST['g_container_man'],
							'address' 		=>	$_POST['g_container_depot'],
							'phone_tel'	=>	$_POST['g_container_tel'],
							'lng'			=>	$_POST['g_container_lng'],
							'lat'			=>	$_POST['g_container_lat'],
							'addrtype'		=>	'集出空站',
							'unit'			=>	$unit,
						);
						$this->addaddr($depot);
					}
					$num1 = $this->checkaddr($_POST['g_full_container_depot'],$userid,$unit);
					if($num1==0){
						$full_depot = array(
							'user_id'    	=>	$userid,
							'consignee' 	=>	$_POST['g_full_container_man'],
							'address' 		=>	$_POST['g_full_container_depot'],
							'phone_tel'	=>	$_POST['g_full_container_tel'],
							'lng'			=>	$_POST['g_full_container_lng'],
							'lat'			=>	$_POST['g_full_container_lat'],
							'addrtype'		=>	'集出重站',
							'unit'			=>	$unit,
						);
						$this->addaddr($full_depot);
					}
					$num2 = $this->checkaddr($_POST['g_loading_place'],$userid,$unit);
					if($num2==0){
						$place = array(
							'user_id'    	=>	$userid,
							'consignee' 	=>	$_POST['g_loading_man'],
							'address' 		=>	$_POST['g_loading_place'],
							'phone_tel'	=>	$_POST['g_loading_tel'],
							'lng'			=>	$_POST['g_loading_lng'],
							'lat'			=>	$_POST['g_loading_lat'],
							'addrtype'		=>	'集出卸货',
							'unit'			=>	$unit,
						);
						$this->addaddr($place);
					}
				}
				if($g_tid){
					//根据货单id取修改前货单的货主
					$sql_old = "select g_companytid from zys_goods_trunk where g_tid=".$g_tid;
					$goods_old = $db->getRow($sql_old);
					$sql="update zys_goods_trunk set g_companytid='".$g_companytid."',g_company='".$operator['o_name']."',g_ship='".$g_ship."',
			g_voyage='".$g_voyage."',g_awb_code='".$g_awb_code."',g_packingtime='".$g_packingtime."',g_tgdate='".$g_tgdate."',g_collectiontime='".$g_collectiontime."',
			g_container_depot='".$g_container_depot."',g_container_lng='".$g_container_lng."',g_container_lat='".$g_container_lat."',g_container_man='".$g_container_man."',g_container_tel='".$g_container_tel."',
			g_full_container_depot='".$g_full_container_depot."',g_full_container_lng='".$g_full_container_lng."',g_full_container_lat='".$g_full_container_lat."',g_full_container_man='".$g_full_container_man."',g_full_container_tel='".$g_full_container_tel."',
			g_loading_lng='".$g_loading_lng."',g_loading_lat='".$g_loading_lat."',g_loading_place='".$g_loading_place."'
			,g_loading_man='".$g_loading_man."',g_loading_tel='".$g_loading_tel."',g_note='".$g_note."' where g_tid=".$g_tid;
					$db->query($sql);
					//运单中创建货单时操作人单位不等于选择货主时，给货主创建一个分享
					if(($g_companytid !=$goods_old['g_companytid']) && ($g_companytid != $unit)){
						//先删除以前的分享再创建新分享
						$sql_del = "delete from zys_sharein where wm_tid='".$id."'and unit='".$goods_old['g_companytid']."'";
						$db->query($sql_del);
						$sql_share="insert into zys_sharein set wm_tid='".$id."',unit='".$g_companytid."',user_id='".$userid."',
				add_time='".gmtime()."'";
						$db->query($sql_share);
					}
				}else{
					$sql="insert into zys_goods_trunk set g_code='T".gmtime()."',g_type='".$wm_type."',g_opdate='".gmtime()."',g_optid='".$this->visitor->get('user_id')."',
			g_opname='".$this->visitor->get('user_name')."',g_companytid='".$g_companytid."',g_company='".$operator['o_name']."',g_ship='".$g_ship."',
			g_voyage='".$g_voyage."',g_awb_code='".$g_awb_code."',g_packingtime='".$g_packingtime."',g_collectiontime='".$g_collectiontime."',
			g_container_depot='".$g_container_depot."',g_container_lng='".$g_container_lng."',g_container_lat='".$g_container_lat."',g_container_man='".$g_container_man."',g_container_tel='".$g_container_tel."',
			g_full_container_depot='".$g_full_container_depot."',g_full_container_lng='".$g_full_container_lng."',g_full_container_lat='".$g_full_container_lat."',g_full_container_man='".$g_full_container_man."',g_full_container_tel='".$g_full_container_tel."',
			g_loading_lng='".$g_loading_lng."',g_loading_lat='".$g_loading_lat."',g_loading_place='".$g_loading_place."'
			,g_loading_man='".$g_loading_man."',g_loading_tel='".$g_loading_tel."',g_note='".$g_note."'";
					$db->query($sql);

					$g_tid=$db->insert_id();
					//运单中创建货单时操作人单位不等于选择货主时，给货主创建一个分享
					if($g_companytid != $unit){
						$sql_share="insert into zys_sharein set wm_tid='".$id."',unit='".$g_companytid."',user_id='".$userid."',
				add_time='".gmtime()."'";
						$db->query($sql_share);
					}
				}

				$data = array(
					'wm_loading_date'=> $g_packingtime,
				    'wm_ship' => $g_ship,
					'wm_voyage'=> $g_voyage,
				    'wm_awb_code'=> $g_awb_code,
					'wm_loading_place'=> $g_loading_place,
				    'wm_loading_date' => $g_packingtime
				);

				$wid = $this->_waybill_mod->edit($id,$data);

				foreach ($g_box as $k=>$v){
					if($gs_tid[$k]){
						$sql="update zys_goods_trunk_sub set g_box='".$v."',g_volume='".$g_volume[$k]."',
				g_ctn_no='".$g_ctn_no[$k]."',g_volumep='".$g_volume[$k]."', gs_gtid='".$g_tid."' where gs_tid=".$gs_tid[$k];
						$db->query($sql);

						$sql="update zys_waybill_sub_trunk set ws_volume='".$g_volume[$k]."' where ws_wmtid='".$id."' and ws_gid='".$g_tid."' and ws_gstid='".$gs_tid[$k]."'";

						$db->query($sql);
					}else{
						$sql="insert into zys_goods_trunk_sub set gs_gtid='".$g_tid."',g_box='".$v."',g_volume='".$g_volume[$k]."',
				g_ctn_no='".$g_ctn_no[$k]."',g_volumep='".$g_volume[$k]."'";

						$db->query($sql);

						$gs_tid=$db->insert_id();

						$sql="insert into zys_waybill_sub_trunk set ws_wmtid='".$id."',ws_gid='".$g_tid."',ws_gstid='".$gs_tid."',
				ws_volume='".$g_volume[$k]."'";

						$db->query($sql);
					}
				}

				$this->show_message('编辑成功', '返回运单',   'index.php?app=waybill&act=edit&id=' . $id

				);
			}else{
				$this->show_warning('不能修改此运单','',   'index.php?app=waybill&act=edit&id='.$id);
				return;
			}
		}
	}
	function index()
	{
		$db=&db();
		$types=array();
		$types['集港'] = '集港';
		$types['疏港'] = '疏港';
		$types['调空箱'] = '调空箱';
		$types['仓库空箱'] = '仓库空箱';

		$this->assign('types', $types);

		$sql="SELECT d_value FROM zys_dictionary WHERE d_code='wstatus'";
		$wstatus=$db->getAll($sql);
		$status=array();
		foreach ($wstatus as  $v){
			$status[$v['d_value']]=$v['d_value'];
		}
		$status[''] = '全部';
		$this->assign('status', $status);

		$type_id=empty($_GET['type_id'])?'':$_GET['type_id'];
		$mstatus=empty($_GET['status'])?'':$_GET['status'];
		$wm_driver=empty($_GET['wm_driver'])?'':$_GET['wm_driver'];
		$wm_vhc_code=empty($_GET['wm_vhc_code'])?'':$_GET['wm_vhc_code'];
		$wm_code=empty($_GET['wm_code'])?'':$_GET['wm_code'];

		$conditions = "1=1 ";
		if($wm_code){
			$conditions.=" and wm_code LIKE '%".$wm_code."%'";
		}
		if($wm_driver){
			$conditions.=" and (wm_driver LIKE '%".$wm_driver."%' OR wm_driver_tel LIKE '%".$wm_driver."%' )";
		}
		if($wm_vhc_code){
			$conditions.=" and wm_vhc_code LIKE '%".$wm_vhc_code."%' ";
		}

		if($type_id){
			$conditions.="  AND wm_type ='".$type_id."'";
		}

		if($mstatus!=null&&$mstatus!=''){
			$conditions.=" and wm_stauts='".$mstatus."' ";
		}
		/*$user_ids = $this->checkpower();
		 if(!empty($user_ids)){
			$conditions.=" and wm_optid in(".$user_ids.")";
			}*/
		//制单时间
		if($_GET['add_time_from']!=''){
			$add_time_from = gmstr2time($_GET['add_time_from']);
			$conditions.=" and wm_opdate >= $add_time_from";
		}
		if($_GET['add_time_to']!=''){
			$add_time_to = gmstr2time_end($_GET['add_time_to']);
			$conditions.=" and wm_opdate <= $add_time_to";
		}
		$page   =   $this->_get_page(10);   //获取分页信息

		$sql2 = "SELECT distinct waybill_main.*
FROM zys_waybill_main waybill_main 
				WHERE  ".$conditions." ORDER BY wm_tid desc LIMIT ".$page['limit'];
		$waybill = $db->getAll($sql2);

		$sql3=" SELECT COUNT(*) FROM (SELECT DISTINCT  waybill_main.*
FROM zys_waybill_main waybill_main 
where $conditions
GROUP BY waybill_main.wm_tid  ) AA   ";

		$page['item_count']=$db->getOne($sql3);   //获取统计数据

		$this->_format_page($page);

		$this->assign('page_info', $page);   //将分页信息传递给视图，用于形成分页条
		$this->assign('waybill', $waybill);
		$this->import_resource(array('script' => 'inline_edit.js,jquery.ui/jquery.ui.js,jquery.ui/i18n/' . i18n_code() . '.js',
                                      'style'=> 'jquery.ui/themes/ui-lightness/jquery.ui.css'));

		$this->assign('tabself', json_encode(array('text'=>'运单管理')));
		$this->display('waybill.index.html');
	}

}